package com.pat.java.base;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class TestServletClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		new Thread(){
			@Override
			public void run() {
				super.run();	
				
				try {
					for(int i=1;i<4;i++){
						this.setName("t1_"+i);
						connect();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
		
		try {
			for(int i=1;i<4;i++){
				Thread.currentThread().setName("main_"+i);
				connect();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		new Thread(){
			@Override
			public void run() {
				super.run();
				
				try {
					for(int i=1;i<4;i++){
						this.setName("t2_"+i);
						connect();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	
	public static void connect() throws Exception{
		String url = "http://localhost:8080/myProj002/action?name=";
		url = url + Thread.currentThread().getName();
		
		URL urlObj = new URL(url);
		HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
		conn.setDoOutput(true);   
		conn.setDoInput(true);
		conn.setUseCaches(false);
		conn.setRequestProperty("Content-type", "application/x-java-serialized-object");
		conn.setRequestMethod("POST");
		conn.connect(); 
		
		InputStream inStrm = conn.getInputStream();
		BufferedReader read = new BufferedReader(new InputStreamReader(inStrm));
		String result = "";
		StringBuilder strBuilder = new StringBuilder();
		while((result = read.readLine()) != null){
			strBuilder.append(result);
			result = read.readLine();
		}
		System.out.println(strBuilder.toString());
	}
}
